package com.ruoyi.project.tool.swagger.UserController;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.system.domain.SysUserService;
import com.ruoyi.project.system.service.ISysUserServiceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户收藏服务Controller
 *
 * @author jjp
 * @date 2020-04-25
 */
@Api(description = "用户收藏服务")
@RestController
@RequestMapping("/mobileUser/service")
public class UserServiceController extends BaseController {
    @Autowired
    private ISysUserServiceService sysUserServiceService;

    /**
     * 查询用户收藏服务列表
     */
    @ApiOperation("查询用户收藏服务列表")
    @GetMapping("/list")
    public TableDataInfo list(SysUserService sysUserService) {
        sysUserService.setMobileUserId(SecurityUtils.getLoginUser().getSysMobileUser().getMobileUserId());
        startPage();
        List<SysUserService> list = sysUserServiceService.selectSysUserServiceList(sysUserService);
        return getDataTable(list);
    }

    /**
     * 获取用户收藏服务详细信息
     */
    @ApiOperation("获取用户收藏服务详细信息")
    @GetMapping(value = "/{typeDetailId}")
    public AjaxResult getInfo(@PathVariable("typeDetailId") Long typeDetailId) {
        return AjaxResult.success(sysUserServiceService.selectSysUserServiceById(typeDetailId));
    }

    /**
     * 新增用户收藏服务
     */
    @ApiOperation("新增用户收藏服务")
    @Log(title = "用户收藏服务", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysUserService sysUserService) {
        sysUserService.setMobileUserId(SecurityUtils.getLoginUser().getSysMobileUser().getMobileUserId());
        List<SysUserService> querymyservice = sysUserServiceService.querymyservice(sysUserService);
        if(querymyservice.size() > 0){
            return AjaxResult.error("已收藏该服务");
        }
        return toAjax(sysUserServiceService.insertSysUserService(sysUserService));
    }

    /**
     * 修改用户收藏服务
     */
    @ApiOperation("修改用户收藏服务")
    @Log(title = "用户收藏服务", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysUserService sysUserService) {
        sysUserService.setMobileUserId(SecurityUtils.getLoginUser().getSysMobileUser().getMobileUserId());
        return toAjax(sysUserServiceService.updateSysUserService(sysUserService));
    }

    /**
     * 删除用户收藏服务
     */
    @ApiOperation("删除用户收藏服务")
    @Log(title = "用户收藏服务", businessType = BusinessType.DELETE)
    @DeleteMapping("/{typeDetailIds}")
    public AjaxResult remove(@PathVariable Long[] typeDetailIds) {
        return toAjax(sysUserServiceService.deleteSysUserServiceByIds(typeDetailIds));
    }
}
